<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Account extends CI_Controller {
    
    
        public function index(){
            if(update_user() == 2){
                $Array_Data['UserInfo'] = $this->session->userdata('UserInfo');
                $this->load->view('user_group2/account_management/account_management_main',$Array_Data);
            }
            else{
                redirect(site_url().'/welcome/not_authorized');
            }
        }
        
        public function fill_in(){
            if(update_user(false,true) === 2){
                $Array_Data['UserInfo'] = $this->session->userdata('UserInfo');
                $this->load->view('user_group2/fill_information',$Array_Data);
            }else{
                $this->load->view('index.html');
            }
        }
        
        public function update_user_information(){
            $this->load->helper('string_addition');
            $CompanyName = $_POST['ComName'];  
            $Websites = strip_string($_POST['Website']);
            $User = $this->session->userdata('UserInfo');
            $Desc = $_POST['Describtion'];
            $AvatarLink = $_POST['AvatarLink'];
            
            $data = array(
                'RelUserEmail'  => $User['UserEmail'],
                'CompanyName'   => $CompanyName, 
                'Websites'      => $Websites,
                'Describtion'   => $Desc,
                'AvatarLink' => $AvatarLink
            );
            
            $this->db->insert(MYSQL_TABLE_PREFLIX.TABLE_USER2_ACCOUNTS_INFORMATION, $data);
            redirect(site_url());
        }
        
        public function upload_avatar(){
            $ext = pathinfo($_FILES['avatar']['name'], PATHINFO_EXTENSION);
            $UserInfo = $this->session->userdata('UserInfo');
            $file_name = md5($UserInfo['UserEmail']).'.'.$ext;
            $target_path = image_url(true).$file_name;

            if(@move_uploaded_file($_FILES['avatar']['tmp_name'], $target_path)) {
               $result = $file_name;
            }
            echo '<script language="javascript" type="text/javascript">window.top.window.StopUpload("'.$result.'");</script>' ;
        }
        
        public function edit_user_account(){
            $UserInfo = $this->session->userdata('UserInfo');
            $AvatarLink = $_POST['AvatarLink'];
            $CompanyName = $_POST['CompanyName'];
            $OldPassword = $_POST['OldPassword'];
            $Describtion = $_POST['Describtion'];
            $changePass = false;
            if(!empty($OldPassword)){
                $NewPassword = $_POST['NewPassword'];
                $changePass = true;
            }
            
            $data = array(
                "CompanyName"   => $CompanyName,
                "AvatarLink"    => $AvatarLink,
                "Describtion"   => $Describtion
            );
            $this->db->where('RelUserEmail',$UserInfo['UserEmail']);
            $this->db->update(MYSQL_TABLE_PREFLIX.TABLE_USER2_ACCOUNTS_INFORMATION,$data);
            
            if($changePass){
                $data = array(
                    'UserPassword' => md5($NewPassword)
                );
                $this->db->where('UserEmail',$UserInfo['UserEmail']);
                $this->db->update(MYSQL_TABLE_PREFLIX.TABLE_USER_ACCOUNTS,$data);
                $UserInfo['UserPassword'] = md5($NewPassword);
                $this->session->set_userdata('UserInfo',$UserInfo);
            }
            redirect(site_url().'/user_group2/account');
        }
        
        public function delete_user_contact(){
            $UserInfo = $this->session->userdata('UserInfo');
            $this->db->delete(MYSQL_TABLE_PREFLIX.TABLE_USER2_CONTACT_INFORMATION,
                    array(
                       'ContactID'  => $_POST['ID'],
                        'RelUserEmail'  => $UserInfo['UserEmail']
                    ));
            
            echo "1";
        }
        
        public function edit_user_contact(){ 
            $this->load->helper('string_addition');
            $UserInfo = $this->session->userdata('UserInfo');
            $Websites = strip_string($_POST['Websites']);
            $this->db->where('RelUserEmail', $UserInfo['UserEmail']);
            $this->db->update(MYSQL_TABLE_PREFLIX.TABLE_USER2_ACCOUNTS_INFORMATION, array('Websites' => $Websites));
            //Address
            $ID = $_POST['currentid'];
            if($ID != -2){
                $Name = $_POST['name'];
                $Address = $_POST['Province'].'#'.$_POST['City'].'#'.$_POST['District']."#".$_POST['Street'];
                $Phones = strip_string($_POST['Phones']);
                $Describtion = $_POST['Describtion'];
                if(preg_match('~iframe.*src="([^"]*)"~', $_POST['GoogleLink'], $GoogleLink))
                    $GoogleLink = $GoogleLink[1];
                else{
                    $GoogleLink = $_POST['GoogleLink'];
                }

                $data = array(
                    "Name"          => $Name,
                    "RelUserEmail"  => $UserInfo["UserEmail"],
                    "Phones"        => $Phones,
                    "Address"       => $Address,
                    "GoogleLink"    => $GoogleLink,
                    "Describtion"   => $Describtion
                );
                if($ID == -1){
                    $this->db->insert(MYSQL_TABLE_PREFLIX.TABLE_USER2_CONTACT_INFORMATION,$data);
                }else{
                    $this->db->where('ContactID',$ID);
                    $this->db->update(MYSQL_TABLE_PREFLIX.TABLE_USER2_CONTACT_INFORMATION,$data);
                }
            }
            redirect(site_url().'/user_group2/account?section=edit-contact');
        }
        
        public function show($ID){
            $CI =& get_instance();
            $Array_Data['UserInfo'] = $this->session->userdata('UserInfo');
            $CI->load->model('Users2_model','Users2');
            $CI->load->model('Users2_ContactInform_model','Us2Contact');
            $Array_Data['DesInfo'] = $CI->Users2->GetInformation(array('UserGroup2ID' => $ID));
            $Array_Data['DesInfo'] = $Array_Data['DesInfo'][0];
            $Array_Data['DesInfo']['Locations'] = $CI->Us2Contact->Get(array('RelUserEmail' => $Array_Data['DesInfo']['RelUserEmail']));
            $this->load->view('user_group2/show_view',$Array_Data);
        }
}
